package com.hmi.simulatedrive.fragment

import android.os.Bundle
import android.text.Editable
import android.text.TextWatcher
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.LinearLayoutManager
import com.amap.api.services.help.Inputtips
import com.amap.api.services.help.Inputtips.InputtipsListener
import com.amap.api.services.help.InputtipsQuery
import com.amap.api.services.help.Tip
import com.blankj.utilcode.util.ToastUtils
import com.hmi.simulatedrive.activity.MainActivity
import com.hmi.simulatedrive.adapter.AMapTipAdapter
import com.hmi.simulatedrive.databinding.FragmentTipSearchBinding


class TipSearchFragment: BaseFragment() {
    var tipSearchBinding: FragmentTipSearchBinding? = null
    private var inputTips: Inputtips? = null
    private var cityCode: String? = null
    private val tipList: MutableList<Tip> = ArrayList()
    private val aMapTipAdapter by lazy { AMapTipAdapter(requireActivity(), tipList) }
    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        tipSearchBinding = FragmentTipSearchBinding.inflate(inflater)
        val name = arguments?.getString("name", "")
        cityCode = arguments?.getString("city", "")!!
        tipSearchBinding!!.edtEnd.setText(name)
        tipSearchBinding!!.edtEnd.isFocusable=true
        tipSearchBinding!!.edtEnd.isFocusableInTouchMode = true
        tipSearchBinding!!.edtEnd.requestFocus()
        initView()
        return tipSearchBinding?.root
    }

    private fun initView() {
        inputTips = Inputtips(requireActivity(), inputtipsListener)
        // 初始化搜索名称控件
        tipSearchBinding!!.edtEnd.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
            override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {}
            override fun afterTextChanged(s: Editable) {
                // 获取修改后的文字名称
                val endStr = s.toString()
                if (cityCode == null) {
                    ToastUtils.showShort("注意，无法获取城市信息！")
                    return
                }
                if (endStr != null && endStr.length > 2) {
                    //第二个参数传入null或者“”代表在全国进行检索，否则按照传入的city进行检索
                    val inputquery = InputtipsQuery(endStr, cityCode)
                    inputquery.cityLimit = true //限制在当前城市
                    inputTips!!.setQuery(inputquery)
                    inputTips!!.requestInputtipsAsyn() // 异步请求提示列表数据
                }
            }
        })
        tipSearchBinding!!.lvSearchTip.adapter = aMapTipAdapter
        val layoutManager = LinearLayoutManager(requireContext())
        tipSearchBinding!!.lvSearchTip.layoutManager = layoutManager
        aMapTipAdapter.setItemClickListener { position, rootView, tip ->
            // 用户点击了指定数据，结束当前界面，返回主界面，将用户选中内容展示在界面上
            (this@TipSearchFragment.requireActivity() as MainActivity).setEndTip(tip)
            this@TipSearchFragment.parentFragmentManager.popBackStack()
        }

        tipSearchBinding!!.tvSearchEnd.setOnClickListener {
            tipSearchBinding!!.edtEnd.setText("")
        }
    }

    private val inputtipsListener =
        InputtipsListener { list, i ->
            tipList.clear()
            // 过滤list数据，如果提示内容为虚拟数据，则过滤之
            val filterList = list.filter {
                return@filter !(it.poiID.isNullOrEmpty() || it.point == null)
            }
            tipList.addAll(filterList)
            aMapTipAdapter.notifyDataSetChanged()
        }


    override fun onResume() {
        super.onResume()
        showInput(tipSearchBinding!!.edtEnd)
    }

    override fun onDestroy() {
        super.onDestroy()
        hideInput()
    }

    override fun onBackPressed(): Boolean {
        parentFragmentManager.popBackStack()
        return true
    }
}